package com.ahora.org.controller.biometrico.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ManualBiometrico")
public class ServletDescargarManualBiometrico extends HttpServlet {
	private static final int DEFAULT_BUFFER_SIZE = 10240; 

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		File file = new File("C:/PAC/ARCHIVOS/BIOMETRICO/MANUAL/Manual.pdf");

        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String contentType = getServletContext().getMimeType(file.getName());

        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(getResponseContentType("Manual.pdf"));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "attachment; filename=\"ManualJavaBiometricoInstalacion.pdf\"");
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            close(output);
            close(input);
        }
	}

	private String getResponseContentType(String contenttype) {

		String responseType = "";

		if (contenttype.indexOf("doc") > -1) {

			responseType = "application/vnd.ms-word";

		}

		if (contenttype.indexOf("rtf") > -1) {

			responseType = "application/rtf";

		}

		else if (contenttype.indexOf("xls") > -1) {

			responseType = "application/vnd.ms-excel";

		}

		else if (contenttype.indexOf("ppt") > -1) {

			responseType = "application/ppt";

		}

		else if (contenttype.indexOf("xml") > -1) {

			responseType = "text/xml";

		}

		else if (contenttype.indexOf("html") > -1) {

			responseType = "text/html";

		}

		else if (contenttype.indexOf("htm") > -1) {

			responseType = "text/htm";

		}

		else if (contenttype.indexOf("txt") > -1
				|| contenttype.indexOf("log") > -1) {

			responseType = "text/plain";

		}

		else if (contenttype.indexOf("pdf") > -1) {

			responseType = "application/pdf";

		}

		else if (contenttype.indexOf("gif") > -1) {

			responseType = "image/gif";

		}

		else if (contenttype.indexOf("png") > -1) {

			responseType = "image/png";

		}

		else if (contenttype.indexOf("jpg") > -1) {

			responseType = "image/jpg";

		}

		else if (contenttype.indexOf("jpeg") > -1) {

			responseType = "image/JPEG";

		}

		else if (contenttype.indexOf("jpe") > -1) {

			responseType = "image/JPEG";

		}

		else if (contenttype.indexOf("bmp") > -1) {

			responseType = "image/bmp";

		}

		else if (contenttype.indexOf("jpeg") > -1) {

			responseType = "image/JPEG";

		}

		else if (contenttype.indexOf("tiff") > -1) {

			responseType = "image/tiff";

		}

		else if (contenttype.indexOf("tif") > -1) {

			responseType = "image/tiff";

		}

		else if (contenttype.indexOf("html") > -1) {

			responseType = "text/html";

		}

		else {

			responseType = "application/octet-stream";

		}

		return responseType;

	}
	
	private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                // Do your thing with the exception. Print it, log it or mail it.
                e.printStackTrace();
            }
        }
    }

}
